package com.timesheet.gui.efforttracker.controllers;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Calendar;
import java.util.Date;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import com.example.timesheet.uitl.DateUtil;
import com.example.timesheet.uitl.TaskUtils;
import com.timesheet.gui.efforttracker.composite.EditNewEffortComposite;
import com.timesheet.gui.efforttracker.utils.EffortTrackerQueryHelper;
import com.timesheet.gui.efforttracker.utils.EffortTrackerUtil;
import com.timesheet.server.common.models.EffortTrackerDTO;
import com.timesheet.server.common.models.view.EffortTrackerViewModel;
import com.timesheet.server.common.models.view.EffortTrackerViewModelContainer;
import com.timesheet.server.common.session.Session;
import com.timesheet.serverice.EffortTrackerService;

public class EditNewEffortCompositeController implements PropertyChangeListener {

	private EffortTrackerViewModelContainer effortTrackerViewModelContainer = EffortTrackerViewModelContainer
			.getInstance();

	private EditNewEffortComposite editNewEffortComposite;

	private EffortTrackerViewModel viewModel;

	EffortTrackerService service = new EffortTrackerService();

	public EditNewEffortCompositeController(
			EditNewEffortComposite editNewEffortComposite) {
		this.editNewEffortComposite = editNewEffortComposite;
		effortTrackerViewModelContainer.addPropertyChangeListener(this);
		performActions();
		// initDataBinding();
	}

	private void performActions() {
		editNewEffortComposite.getBtnSave().addSelectionListener(
				new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {
						update();
					}
				});
	}

	protected void update() {
		if (viewModel == null) {
			throw new RuntimeException("model cannot be null");
		}
		update2();

		EffortTrackerDTO effortTrackerDto = new EffortTrackerDTO();
		EffortTrackerDTO convertViewModel2Dto = EffortTrackerUtil
				.convertViewModel2Dto(viewModel, effortTrackerDto);
		service.update(convertViewModel2Dto);
		effortTrackerViewModelContainer.setList(new EffortTrackerQueryHelper()
				.loadDailyEfforts("ABSLIFE", Session.INSTANCE.getUser(),
						effortTrackerViewModelContainer.getWeekDate()));
		effortTrackerViewModelContainer.refresh();

	}

	@Override
	public void propertyChange(PropertyChangeEvent arg0) {

		viewModel = effortTrackerViewModelContainer.getEffortTrackerViewModel();
		// if (newValue instanceof EffortTrackerViewModel) {

		if (viewModel != null) {
			//throw new RuntimeException("View model is null");

			editNewEffortComposite.getTxtHours().setText(
					String.valueOf(viewModel.getHours()));
			editNewEffortComposite.getTxtDescription().setText(
					viewModel.getTaskDescription());
			editNewEffortComposite.getTxtTaskNo()
					.setText(viewModel.getTaskNo());
			editNewEffortComposite.getCmbStatus().setItems(
					TaskUtils.getTaskStatus());
			editNewEffortComposite.getCmbStatus()
					.setText(viewModel.getStatus());

			editNewEffortComposite.getCmbTaskPhase().setItems(
					TaskUtils.getTaskPhases());

			editNewEffortComposite.getCmbTaskPhase().setText(
					viewModel.getTaskPhase());

			// editNewEffortComposite.getCmbTaskType().set
			// editNewEffortComposite.getCmbTaskType().setListVisible(true);

			editNewEffortComposite.getCmbTaskType().setItems(
					TaskUtils.getTaskTypes());
			editNewEffortComposite.getCmbTaskType().setText(
					viewModel.getTaskType());

			Date dayDate = viewModel.getDayDate();
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(dayDate);
			editNewEffortComposite.getDateTime().setDate(
					calendar.get(Calendar.DAY_OF_WEEK),
					calendar.get(Calendar.MONTH), calendar.get(Calendar.YEAR));
			// if (!hasBound || (arg0.getPropertyName().equals("notify"))) {
			System.out.println("Bound");
		}
	}

	private void update2() {
		if (viewModel == null) {
			throw new RuntimeException("view model null");
		}
		viewModel.setHours(Double.valueOf(editNewEffortComposite.getTxtHours()
				.getText()));
		viewModel.setTaskDescription(editNewEffortComposite.getTxtDescription()
				.getText());
		viewModel.setTaskNo(editNewEffortComposite.getTxtTaskNo().getText());
		viewModel.setStatus(editNewEffortComposite.getCmbStatus().getText());
		viewModel.setTaskPhase(editNewEffortComposite.getCmbTaskPhase()
				.getText());
		viewModel
				.setTaskType(editNewEffortComposite.getCmbTaskType().getText());
		int day = editNewEffortComposite.getDateTime().getDay();
		int month = editNewEffortComposite.getDateTime().getMonth();
		int year = editNewEffortComposite.getDateTime().getYear();
		viewModel.setDayDate(DateUtil.createDate(day, month, year));
	}

}
